# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
from typing import List

from fastapi import Body
from app.common import logger

from app.common.concurrency import async_route
from app.common.exter_attack import exter_attack
from app.common.schemas.job_schemas import JobSchema
from app.ai_sorter.service import train_start, reschedule_jobs

log = logger.get_logger(__name__)
ai_sorter_router = async_route()
API_TAG = ["AiSorter"]


@exter_attack
@ai_sorter_router.post(
    "/v1/internal/aisorter",
    status_code=200,
    tags=API_TAG,
    description="Re schedule jobs",
    summary="AI调度对job进行重新排序"
)
def reschedule(
        job_list: List[JobSchema] = Body(None, description="List of Jobs")
) -> List[JobSchema]:
    log.info(f"start to rearrange the jobs.")
    return reschedule_jobs(job_list)


@exter_attack
@ai_sorter_router.get(
    "/v1/internal/aisorter/retrain",
    status_code=200,
    tags=API_TAG,
    description="Re train the AI model",
    summary="模型训练"
)
def retrain():
    log.info(f"start to train the model.")
    train_start()
